#include "dvbinner.h"

typedef struct
{
	unsigned int	m_u32Version;
	unsigned int	m_u32Size;		//structure size
	unsigned int	m_u32FirstBoot;
	unsigned int	m_u32CurrentSource;//TUNER_T    0, TUNER_C    1, TUNER_S     2
	dvb_tuner_t	m_stMainFreq1;
	dvb_tuner_t	m_stMainFreq2;
	dvb_tuner_t	m_stNitFreq;
	char			m_s8NetName[DVB_STRING_MAX_LEN];
	unsigned int	m_u32NetID;
	char			m_s8PluginPath[DVB_STRING_MAX_LEN];
	unsigned int	m_u32Crc;
}DVBUSERINFO;

DVBUSERINFO DvbUserInfo;

int SaveUserInfo()
{
	int ret;
	unsigned int u32Crc;

	DvbUserInfo.m_u32Crc = 0;
	DvbUserInfo.m_u32Crc = DvbGetCRC32((unsigned char*)&DvbUserInfo, sizeof(DvbUserInfo)-4);
	ret = mwNvmWrite(FLASH_USER_CONFIG, 0, (unsigned char*)&DvbUserInfo,  sizeof(DVBUSERINFO));
	if(ret != 0)
	{
		mwError("SaveUserInfo() - failed");
	}
	return ret;
}

int RestoreUserInfo()
{
	memset(&DvbUserInfo, 0, sizeof(DVBUSERINFO));
	DvbUserInfo.m_u32Version = 0x01;
	DvbUserInfo.m_u32Size = sizeof(DvbUserInfo);
	DvbUserInfo.m_u32FirstBoot = 1;
	DvbUserInfo.m_u32CurrentSource = TUNER_C;
	DvbUserInfo.m_u32NetID = 0;
	
	DvbUserInfo.m_stMainFreq1.frequency = 259000;
	DvbUserInfo.m_stMainFreq1.symbol_rate = 6875;
	DvbUserInfo.m_stMainFreq1.modulation = 64;
	DvbUserInfo.m_stMainFreq1.bandwidth = 8;
	DvbUserInfo.m_stMainFreq1.tunertype = TUNER_C;

	DvbUserInfo.m_stMainFreq2.frequency = 576000;
	DvbUserInfo.m_stMainFreq2.symbol_rate = 0;
	DvbUserInfo.m_stMainFreq2.modulation = 0;
	DvbUserInfo.m_stMainFreq2.bandwidth = 8;
	DvbUserInfo.m_stMainFreq2.tunertype = TUNER_T;
	SaveUserInfo();
}

int LoadUserInfo()
{
	int ret;
	unsigned int u32Crc;
	
	memset(&DvbUserInfo, 0, sizeof(DVBUSERINFO));
	ret=mwNvmRead(FLASH_USER_CONFIG, 0, (unsigned char*)&DvbUserInfo, sizeof(DVBUSERINFO));
	if(ret == 0)
	{
		u32Crc = DvbGetCRC32((unsigned char*)&DvbUserInfo, sizeof(DVBUSERINFO)-4);
		if(u32Crc != DvbUserInfo.m_u32Crc)
		{
			mwError("LoadUserInfo() - crc check fail\r\n");
			RestoreUserInfo(&DvbUserInfo);
		}
	}
	else
	{
		mwError("LoadUserInfo() - failed");
		RestoreUserInfo(&DvbUserInfo);
	}

	return ret;
}

int DVBSetMainFreq(dvb_tuner_t* pFreqParams)//khz
{
	int ret;
	if(pFreqParams->tunertype==TUNER_T)
	{
		memcpy(&DvbUserInfo.m_stMainFreq2, pFreqParams, sizeof(dvb_tuner_t));
	}
	else if(pFreqParams->tunertype==TUNER_C)
	{
		memcpy(&DvbUserInfo.m_stMainFreq1, pFreqParams, sizeof(dvb_tuner_t));
	}
	ret=SaveUserInfo();
	
	return ret;
}

int DVBGetMainFreq(dvb_tuner_t* pFreqParams)
{
	int ret = 0;

	if(pFreqParams->tunertype==TUNER_T)
	{
		memcpy(pFreqParams, &DvbUserInfo.m_stMainFreq2, sizeof(dvb_tuner_t));
	}
	else if(pFreqParams->tunertype==TUNER_C)
	{
		memcpy(pFreqParams, &DvbUserInfo.m_stMainFreq1, sizeof(dvb_tuner_t));
	}
	
	return 0;
}

int DVBGetFirstBootFlag(unsigned int* pu32Flag)
{
	*pu32Flag = DvbUserInfo.m_u32FirstBoot;
	return 0;
}

int DVBSetFirstBootFlag(unsigned int u32Flag)
{
	DvbUserInfo.m_u32FirstBoot = u32Flag;
	SaveUserInfo();
	
	return 0;
}

int DVBGetNitFreq(dvb_tuner_t* pstFreqParams)
{
	memcpy(pstFreqParams, &DvbUserInfo.m_stNitFreq, sizeof(dvb_tuner_t));
	return 0;
}

int DVBSetNitFreq(dvb_tuner_t* pstFreqParams)
{
	mwDebug("DVBSetNitFreq()  - pstFreqParams->frequency = %d\r\n", pstFreqParams->frequency);
	memcpy(&DvbUserInfo.m_stNitFreq, pstFreqParams, sizeof(dvb_tuner_t));
	SaveUserInfo();
	
	return 0;
}

int DVBGetCurrentSource(unsigned int* pu32Source)
{
	*pu32Source = DvbUserInfo.m_u32CurrentSource;
	return 0;
}

int DVBSetCurrentSource(unsigned int u32Source)
{
	DvbUserInfo.m_u32CurrentSource = u32Source;
	SaveUserInfo();
	
	return 0;
}

int DVBGetOperatorInfo(dvb_operator_info_t* pstOperatorInfo)
{
	memcpy(pstOperatorInfo->m_s8NetName, DvbUserInfo.m_s8NetName, sizeof(DvbUserInfo.m_s8NetName));
	pstOperatorInfo->m_u32NetID = DvbUserInfo.m_u32NetID;
	return 0;
}

int DVBSetOperatorInfo(dvb_operator_info_t* pstOperatorInfo)
{
	memcpy(DvbUserInfo.m_s8NetName, pstOperatorInfo->m_s8NetName, sizeof(DvbUserInfo.m_s8NetName));
	DvbUserInfo.m_u32NetID = pstOperatorInfo->m_u32NetID;
	SaveUserInfo();
	
	return 0;
}

const char* DVBGetPluginPath()
{
	return DvbUserInfo.m_s8PluginPath;
}

int DVBSetPluginPath(const char* ps8Path)
{
	int s32Ret = -1;
	
	if(ps8Path && strlen(ps8Path)<sizeof(DvbUserInfo.m_s8PluginPath))
	{
		memset(DvbUserInfo.m_s8PluginPath, 0, sizeof(DvbUserInfo.m_s8PluginPath));
		memcpy(DvbUserInfo.m_s8PluginPath, ps8Path, strlen(ps8Path));
		SaveUserInfo();
	}

	return s32Ret;
}
